package org.kabeja.parser;

import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.entity.DXFBlock;
import org.kabeja.dxf.entity.DXFEntity;

/**
 * @author shutao
 * @since 2022/4/25 14:22
 */
public class DXFBlocksSectionHandler extends DXFEntitiesSectionHandler {
    public static final String SECTION_KEY = "BLOCKS";
    public static final String BLOCK_START = "BLOCK";
    public static final String BLOCK_END = "ENDBLK";
    public static final int BLOCK = 0;
    public static final int BLOCK_NAME = 2;
    public static final int BLOCK_NAME2 = 3;
    public static final int BLOCK_DESCRIPTION = 4;
    public static final int BLOCK_XREFPATHNAME = 8;
    public static final int BLOCK_BASE_X = 10;
    public static final int BLOCK_BASE_Y = 20;
    public static final int BLOCK_BASE_Z = 30;
    protected boolean parseBlockHeader = false;
    private DXFBlock block;

    public DXFBlocksSectionHandler() {
    }

    @Override
    public String getSectionKey() {
        return SECTION_KEY;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch(var1) {
            case BLOCK:
                if (BLOCK_START.equals(var2.getValue())) {
                    this.parseBlockHeader = true;
                    this.block = new DXFBlock();
                } else if (BLOCK_END.equals(var2.getValue())) {
                    this.endEntity();
                    this.doc.addDXFBlock(this.block);
                } else {
                    this.parseBlockHeader = false;
                    super.parseGroup(var1, var2);
                }
                break;
            case BLOCK_NAME:
                if (this.parseBlockHeader) {
                    this.block.setName(var2.getValue());
                } else {
                    super.parseGroup(var1, var2);
                }
                break;
            case BLOCK_NAME2:
                if (!this.parseBlockHeader) {
                    super.parseGroup(var1, var2);
                }
                break;
            case BLOCK_DESCRIPTION:
                if (this.parseBlockHeader) {
                    this.block.setDescription(var2.getValue());
                } else {
                    super.parseGroup(var1, var2);
                }
                break;
            case BLOCK_XREFPATHNAME:
                if (this.parseBlockHeader) {
                    this.block.setLayerID(var2.getValue());
                } else {
                    super.parseGroup(var1, var2);
                }
                break;
            case BLOCK_BASE_X:
                if (this.parseBlockHeader) {
                    this.block.getReferencePoint().setX(var2.getDoubleValue());
                } else {
                    super.parseGroup(var1, var2);
                }
                break;
            case BLOCK_BASE_Y:
                if (this.parseBlockHeader) {
                    this.block.getReferencePoint().setY(var2.getDoubleValue());
                } else {
                    super.parseGroup(var1, var2);
                }
                break;
            case BLOCK_BASE_Z:
                if (this.parseBlockHeader) {
                    this.block.getReferencePoint().setZ(var2.getDoubleValue());
                } else {
                    super.parseGroup(var1, var2);
                }
                break;
            default:
                super.parseGroup(var1, var2);
        }

    }

    @Override
    public void setDXFDocument(DXFDocument var1) {
        super.setDXFDocument(var1);
    }

    @Override
    public void startSection() {
        this.parseEntity = false;
    }

    @Override
    public void endSection() {
    }

    @Override
    protected void endEntity() {
        if (this.parseEntity) {
            this.handler.endDXFEntity();
            DXFEntity var1 = this.handler.getDXFEntity();
            this.block.addDXFEntity(var1);
            this.parseEntity = false;
        }

    }
}